package com.bridge.common.pay;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

@Data
@Component
public class WXPayConfigImpl extends WXPayConfig {

    /** appId ： 微信公众号或者小程序等的appid */
    @Value("${wechat.official.appId:wxcdb14cbb3390f529}")
    private String appId;
    /** appId ： 微信公众号或者小程序等的appid */
    @Value("${wechat.official.appSecret:9536ef4c6710df008acbaa291d5796f6}")
    private String appSecret;

    /** mchKey ： 商户证书目录 */
    @Value("${wechat.official.notifyUrl:https://jiamei.omnrj.com/applets/pay/back}")
    private String notifyUrl;

    /** mchId ： 微信支付商户号 */
    @Value("${wechat.pay.mchId:1623918231}")
    private String mchId;
    /** mchKey ： 微信支付商户密钥 */
    @Value("${wechat.pay.mchKey:mHXMjpV9JyBmeR8w2gyZveSNrxDDhezx}")
    private String key;
    /** mchKey ： 商户证书目录 */
    @Value("${wechat.pay.keyPath:}")
    private String keyPath;

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKeyPath() {
        return keyPath;
    }

    public void setKeyPath(String keyPath) {
        this.keyPath = keyPath;
    }

    private WXPayConfigImpl() throws Exception{

    }



    public InputStream getCertStream() {
        File file = new File(keyPath);
        try {
            InputStream certStream = new FileInputStream(file);
            byte [] certData = new byte[(int) file.length()];
            certStream.read(certData);
            certStream.close();
            ByteArrayInputStream certBis;
            certBis = new ByteArrayInputStream(certData);
            return certBis;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    public int getHttpConnectTimeoutMs() {
        return 2000;
    }
 
    public int getHttpReadTimeoutMs() {
        return 10000;
    }


    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }
 
    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }
 
    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }
 
    @Override
    public int getReportWorkerNum() {
        return 1;
    }
 
    @Override
    public int getReportBatchSize() {
        return 2;
    }
}